package com.xb.xbpm.service.impl;


import com.xb.xbpm.mapper.PvMapper;
import com.xb.xbpm.mapper.SiteMapper;
import com.xb.xbpm.service.IDataService;
import com.xb.xbpm.utils.Constants;
import com.xb.xbpm.utils.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author gxd
 * @Date： 2018/7/25
 */
@Service
public class DataService implements IDataService {
    @Autowired
    PvMapper pvMapper;


    @Override
    public List<Map<String, Object>> getCountDataList(int type,String userid, String siteid, int timeType, String startDate, String endDate) {
        Map<String, String> map = new HashMap();
        map.put("lmid", userid);
        map.put("siteid", siteid);
        map.put("startDate", startDate);
        List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();
        if(timeType==0){
            //小时间隔

            if(type==Constants.COUNT_PV){
                list=pvMapper.getPvListByHour(map);
            }else if(type==Constants.COUNT_UV){
                list=pvMapper.getUvListByHour(map);
            }else if(type==Constants.COUNT_IPS){
                list=pvMapper.getIpsListByHour(map);
            }else if(type==Constants.COUNT_NEWUV){
                list=pvMapper.getNewUvListByHour(map);
            }else{
                return list;
            }

            for (Map map1: list) {
                map1.put("day",map1.get("time")+":00");
            }
            return list;
        }else{
            //日期间隔
            map.put("endDate", endDate);

            if(type==Constants.COUNT_PV){
                return pvMapper.getPvListByDay(map);
            }else if(type==Constants.COUNT_UV){
                return pvMapper.getUvListByDay(map);
            }else if(type==Constants.COUNT_IPS){
                return pvMapper.getIpsListByDay(map);
            }else if(type==Constants.COUNT_NEWUV){
                return pvMapper.getNewUvListByDay(map);
            }else{
                return list;
            }

        }

    }

    @Override
    public long getTotalCountPv(String userid, String siteid, int timeType, String startDate, String endDate) {
        Map<String, String> map = new HashMap();
        map.put("lmid", userid);
        map.put("siteid", siteid);
        map.put("startDate", startDate);
        if(timeType==0) {
            map.put("endDate", startDate);
        }else{
            map.put("endDate", endDate);
        }
        Map<String,Object> map1=pvMapper.getTotalCountPv(map);
        if(map1 ==null){
            return 0;
        }
        Double count =(Double) map1.get("count");
        return count.longValue();

    }

    @Override
    public long getTotalCountUv(String userid, String siteid, int timeType, String startDate, String endDate) {
        Map<String, String> map = new HashMap();
        map.put("lmid", userid);
        map.put("siteid", siteid);
        map.put("startDate", startDate);
        if(timeType==0) {
            map.put("endDate", startDate);
        }else{
            map.put("endDate", endDate);
        }

        Map<String,Object> map1=pvMapper.getTotalCountUv(map);
        if(map1 ==null){
            return 0;
        }
        Double count =(Double) map1.get("count");
        return count.longValue();
    }

    @Override
    public long getTotalCountIps(String userid, String siteid, int timeType, String startDate, String endDate) {
        Map<String, String> map = new HashMap();
        map.put("lmid", userid);
        map.put("siteid", siteid);
        map.put("startDate", startDate);
        if(timeType==0) {
            map.put("endDate", startDate);
        }else{
            map.put("endDate", endDate);
        }

        Map<String,Object> map1=pvMapper.getTotalCountIps(map);
        if(map1 ==null){
            return 0;
        }
        Double count =(Double) map1.get("count");
        return count.longValue();
    }

    @Override
    public long getTotalCountNewUv(String userid, String siteid, int timeType, String startDate, String endDate) {
        Map<String, String> map = new HashMap();
        map.put("lmid", userid);
        map.put("siteid", siteid);
        map.put("startDate", startDate);
        if(timeType==0) {
            map.put("endDate", startDate);
        }else{
            map.put("endDate", endDate);
        }


        Map<String,Object> map1=pvMapper.getTotalCountNewUv(map);
        if(map1 ==null){
            return 0;
        }
        Double count =(Double) map1.get("count");
        return count.longValue();
    }


}
